/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.boundary;

import it.polimi.registration.business.security.entity.BadWeatherNotification;
import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.MyForecast;
import it.polimi.registration.business.security.entity.Users;
import it.polimi.registration.business.security.entity.UsersEvent;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 *
 * @author Luca
 */
@Stateless
public class BadWeatherManager {

    @PersistenceContext
    EntityManager em;

    @Inject
    Principal principal;

    public void save(BadWeatherNotification notification) {

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<BadWeatherNotification>> constraintViolations = validator.validate(notification);
        if (constraintViolations.size() > 0) {
            Iterator<ConstraintViolation<BadWeatherNotification>> iterator = constraintViolations.iterator();
            while (iterator.hasNext()) {
                ConstraintViolation<BadWeatherNotification> cv = iterator.next();
                System.err.println(cv.getRootBeanClass().getName() + "." + cv.getPropertyPath() + " " + cv.getMessage());

                //JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName() + "." + cv.getPropertyPath() + " " + cv.getMessage());
            }
        } else {
            em.persist(notification);
        }
    }

    public void delete() {
        em.remove(getSelectedNotification());
    }

    private Object getSelectedNotification() {
        return em.find(Event.class, principal.getName());
    }

    public List<Event> loadNotifications(Users users) {
        List<Event> result = new ArrayList<>();
        Query query = em.createQuery("SELECT ue From MyForecast f,UsersEvent ue Where (f.location.city=ue.event.location.city AND f.date= CAST(ue.event.endDate as DATE) AND ue.users= :users) ");
        query.setParameter("users", users);
        List<UsersEvent> eventLoaded = new ArrayList<>(query.getResultList());

        for (int i = 0; i < eventLoaded.size(); i++) {
            Query query2 = em.createQuery("SELECT f From MyForecast f Where (f.location.city= :city AND f.date= CAST(:date as DATE))");
            query2.setParameter("city", eventLoaded.get(i).getEvent().getLocation().getCity());
            query2.setParameter("date", eventLoaded.get(i).getEvent().getEndDate());
            List<MyForecast> MyFList;
            MyFList = new ArrayList<>(query2.getResultList());
            if (!MyFList.isEmpty()) {
                MyForecast myF = MyFList.get(0);
                if (myF.getDescription().contains("rain")) {
                    if (eventLoaded.get(i).getCreator() || eventLoaded.get(i).getAccepted()) {

                        result.add(eventLoaded.get(i).getEvent());
                    }
                }
            }
        }
        return (List) result;
    }
}
